import 'package:flutter/cupertino.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年05月20日 14:26
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

class InputDialog extends StatefulWidget {
  final String? title;
  final String? textString;
  final Function(String?) callBack;
  const InputDialog({required this.title,required this.textString,required this.callBack, super.key});

  @override
  State<InputDialog> createState() => _InputDialogState();
}

class _InputDialogState extends State<InputDialog> {
  final TextEditingController _textEditingController = TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _textEditingController.text = widget.textString??"";
  }
  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      //自定义dialog布局
      child: Center(
          //保证控件居中效果

          child: Column(
        children: <Widget>[
          Container(
            // width: ScreenUtil().setWidth(540),
            height: 178,
            margin: EdgeInsets.only(
                left: 38.w,
                right: 38.w,
                top: 320.h - ScreenUtil().statusBarHeight),
            decoration: const BoxDecoration(
                color: Colour.white,
                borderRadius: BorderRadius.all(Radius.circular(8.0))),
            child: Column(
              children: <Widget>[
                //标题
                Padding(
                  padding: const EdgeInsets.only(top: 26),
                  child: Text(
                    widget.title.toString(),
                    style:
                        ytTextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                  ),
                ),
                //内容

                Container(
                  margin: EdgeInsets.only(
                      left: 24.w, right: 24.w, top: 12, bottom: 28),
                  //修饰黑色背景与圆角
                  decoration: BoxDecoration(
                    border: Border.all(
                        color: Colour.dividerColor, width: 0.5), //灰色的一层边框

                    borderRadius: const BorderRadius.all(Radius.circular(4.0)),
                  ),
                  // alignment: Alignment.center,
                  height: 40,
                  // width: ScreenUtil().setWidth(300),
                  padding: EdgeInsets.only(left: 5, right: 5),
                  child: TextField(
                    controller: _textEditingController,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      contentPadding: const EdgeInsets.only(top: 0, bottom: 6),
                      hintText: ResKey.plsInput.tr,
                      enabled: true,
                      hintStyle: ytTextStyle(
                        fontSize: 13,
                        color: Colour.hintTextColor,
                        fontWeight: FontWeight.normal,
                      ),
                    ),

                    // inputFormatters: <TextInputFormatter>[
                    //   LengthLimitingTextInputFormatter(6)//限制长度
                    // ],
                    style:
                        ytTextStyle(fontSize: 14, color: Colour.otherTextColor),
                  ),
                ),
                Container(
                  height: 0.5,
                  color: Colour.dividerColor,
                ),
                Container(
                  height: 46,
                  child: Row(
                    children: [
                      Expanded(
                          child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        child: Container(
                          child: Center(
                            child: Text(
                              ResKey.cancel.tr,
                              style: ytTextStyle(
                                  color: Colour.mainTextColor,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500),
                            ),
                          ),
                        ),
                        onTap: () { Get.back();},
                      )),
                      Container(
                        height: 36,
                        width: 0.5,
                        color: Colour.dividerColor,
                      ),
                      Expanded(
                          child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        child: Center(
                          child: Text(
                            ResKey.sure.tr,
                            style: ytTextStyle(
                                color: Colour.primaryColor,
                                fontSize: 16,
                                fontWeight: FontWeight.w500),
                          ),
                        ),
                        onTap: () {
                          if(_textEditingController.text.isNotEmpty){
                            widget.callBack(_textEditingController.text);
                            Get.back();
                          }else{
                            EasyLoading.showError(ResKey.plsInput.tr + ResKey.deviceName.tr);
                          }
                        },
                      ))
                    ],
                  ),
                ),
              ],
            ),
          ),
          //按钮区
        ],
      )),
    );
  }
}
